package com.example.newproject_overseas.Screen.Level


import android.content.Context
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.newproject_overseas.ViewModel.JobViewModel
import com.example.newproject_overseas.components.SelectedObject
import com.example.newproject_overseas.components.TopApp

@Composable
fun OneLevel(navController: NavHostController,jobViewModel: JobViewModel = viewModel()) {

    var occ1Name by remember { mutableStateOf("") }
    var occ2Name by remember { mutableStateOf("") }
    var occ1Id by remember { mutableStateOf("") }
    var occ2Id by remember { mutableStateOf("") }
    var occVal by remember { mutableStateOf("") }
    var occBool by remember { mutableStateOf(false) }


    //网络请求
    LaunchedEffect(Unit){
        //获取地址信息
        jobViewModel.addressReq(navController)
    }
    val addressModel = jobViewModel.addressModel

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color(0XFFEFF0F3)),

        ) {

        TopApp(title = "Work information", navController = navController)

        Column(
            modifier = Modifier
                .height(250.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            if (addressModel != null) {
                LazyColumn(modifier = Modifier) {
                    items(addressModel) { data ->
                        Divider()
                        Row(modifier = Modifier
                            .clickable {

                                occ1Name = data.name;
                                occ1Id = data.id

                                navController.navigate("TwoLevel/${occ1Id}")
//                            selectedProvinceIndex = provinces.indexOf(data)
                            }
                            .padding(top = 10.dp, bottom = 10.dp)
                            .fillMaxWidth(),
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.Center
                        ) {
                            Text(text = data.name)
                        }


//                    else if (selectedProvinceIndex == provinces.indexOf(data) && occBool == true) {
//                        //2级
//                        data.children.forEach { city ->
//                            Divider()
//                            Row(modifier = Modifier
//                                .clickable {
//                                    occ2Id = city.id
//                                    occ2Name = city.name;
//                                    occVal = occ1Name + occ2Name //最终结果
//                                    occBool = false;
//                                    scope.launch { state.hide() }
//
//                                    val selectedObject = SelectedObject(
//                                        id1 = occ1Id,
//                                        name1 = occ1Name,
//                                        id2 = occ2Id,
//                                        name2 = occ2Name,
//                                        name = "$occ1Name$occ2Name"
//                                    )
//
//
//                                    onItemSelected(selectedObject)
//                                }
//                                .padding(top = 10.dp, bottom = 10.dp)
//                                .fillMaxWidth(),
//                                verticalAlignment = Alignment.CenterVertically,
//                                horizontalArrangement = Arrangement.Center
//                            ) {
//                                Text(text = city.name)
//                            }
//                        }
//
//                    }


                    }


                }
            }
        }
    }
}


@Preview
@Composable
fun OneLevelPreview() {
    val navController = rememberNavController() // 假设这里使用了导航控制器
    OneLevel(navController)
}

